﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppEquipmentManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class PlanarProcessServiceMapController : BaseController
    {
        private readonly PlanarProcessServiceMapApp _planarProcessServiceMapApp;
        private readonly EquipmentApp _equipmentApp;

        public PlanarProcessServiceMapController(PlanarProcessServiceMapApp planarProcessServiceMapApp, EquipmentApp equipmentApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._planarProcessServiceMapApp = planarProcessServiceMapApp;
            this._equipmentApp = equipmentApp;
        }

        public IActionResult Index()
        {
            return View();
        }
        ///// <summary>
        ///// 平面设备报文处理页面
        ///// </summary>
        ///// <returns></returns>
        //public async Task<ActionResult> PlanarProceServiceMapInfoAsync()
        //{
        //    await AddTracesInfo();
        //    return View();
        //}

        /// <summary>
        /// 平面设备报文处理页面
        /// </summary>
        /// <returns></returns>
        public  ActionResult PlanarProceServiceMapInfoAsync()
        {
            return View();
        }

        /// <summary>
        /// 平面设备报文处理获取所有列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllPlanarProceServiceMapInfoAsync()
        {
            var where = new QueryPlanarProcessServiceMapInfoDto();
            where.PlanarProcessServiceName = Request.Form["PlanarProcessServiceName"].ToString();
            where.EquipmentCode = Request.Form["EquipmentCode"].ToString();
            var planarProceServiceMap = await _planarProcessServiceMapApp.GetPlanarProcessServiceMapAsync(where);

            var pageData = new TableData<PlanarProcessServiceMapInfoDto>(Draw, planarProceServiceMap.Count, planarProceServiceMap.Count, planarProceServiceMap);

            //await AddTracesInfo(5, 0, "获取平面设备报文处理列表");
            return Json(pageData);
        }
        /// <summary>
        /// 添加或者编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> PlanarProcesServiceMapInfoAddOrEditAsync(long id)
        {
            var pageData = new PlanarProcesServiceMapPageData();
            //获取设备列表用于选择下拉
            var equipments = await _equipmentApp.GetALlDispatchEquipmentAsync();
            pageData.EquipmentSearchDatas = new List<DispatchEquipmentSearchData>();
            if (equipments != null && equipments.Count > 0)
            {
                foreach (var item in equipments)
                {
                    pageData.EquipmentSearchDatas.Add(new DispatchEquipmentSearchData() { EquipmentCode = item.EquipmentCode });    
                }
            }
            if(id > 0)
            {
                var planarPro = await _planarProcessServiceMapApp.GetPlanarProcessServiceMapByIDAsync(id);
                if(planarPro != null)
                {
                    pageData.ID = planarPro.ID;
                    pageData.PlanarProcessServiceName = planarPro.PlanarProcessServiceName;
                    pageData.EquipmentCode = planarPro.EquipmentCode;
                    pageData.PlcActionType = planarPro.PlcActionType;
                    pageData.WcsActionType = planarPro.WcsActionType;
                }
            }
            return View(pageData);
        }

        /// <summary>
        /// 添加或者编辑操作
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperatePlanarProceServiceMapInfoAsync()
        {
            var planarProcDto = new PlanarProcessServiceMapInfoDto();
            planarProcDto.ID = Request.Form["id"].ToInt64();
            planarProcDto.EquipmentCode = Request.Form["EquipmentCode"].ToString();
            planarProcDto.PlcActionType = (short)Request.Form["PlcActionType"].ToInt32();
            planarProcDto.WcsActionType = (short)Request.Form["WcsActionType"].ToInt32();
            planarProcDto.PlanarProcessServiceName = Request.Form["PlanarProcessServiceName"].ToString();

            bool result;
            string msg;
            try
            {
                if (planarProcDto.ID == 0)
                {
                    var res = await _planarProcessServiceMapApp.AddPlanarProcessServiceMapAsync(planarProcDto);
                    await AddTracesInfo(2, 0, "新增平面设备报文处理配置");
                    result = res > 0;
                }
                else
                {
                    result = await _planarProcessServiceMapApp.UpdatePlanarProcessServiceMapAsync(planarProcDto);
                    await AddTracesInfo(3, 0, "修改平面设备报文处理配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelPlanarProceServiceMapInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _planarProcessServiceMapApp.DeletePlanarProcessServiceMapAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除平面设备报文处理配置");
            return CreateJsonGetResult(result, msg);
        }

    }
}
